package com.niji.littly_donkey_android.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.*;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.niji.littly_donkey_android.R;
import com.niji.littly_donkey_android.adapter.BillDetailsAdapter;
import com.niji.littly_donkey_android.adapter.BillWithdrawalAdapter;
import com.niji.littly_donkey_android.adapter.FollowAdapter;
import com.niji.littly_donkey_android.bean.BillDetails;
import com.niji.littly_donkey_android.bean.Follow;
import com.niji.littly_donkey_android.http.ConnectViaSession;
import com.niji.littly_donkey_android.util.JsonUtil;
import com.niji.littly_donkey_android.util.StatusBarUtil;
import com.niji.littly_donkey_android.util.TimeUtil;
import com.niji.littly_donkey_android.util.UrlUtil;
import org.json.JSONException;

import java.util.*;

public class BillDetailsActivity extends BaseActivity {

    private static final String TAG = "BillDetailsActivity";

    /**
     * 返回
     */
    private Button bill_details_return;

    /**
     * 收益明细列表
     */
    private ListView bill_details_list;

    /**
     * 列表
     */
    private ArrayList<BillDetails> billDetails;

    private TextView bill_details_text;

    private TextView bill_withdrawal_text;

    private ImageView bill_details_img;

    private ImageView bill_withdrawal_img;
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bill_details_activity);
        init();
        billDetailsReturn();
        billWithdrawalChange();
        billDetailsChange();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.bill_details_activity;

    }
    @Override
    protected int getStatusBarColor() {
        return R.color.white_easy_photos;
    }

    /**
     * 展示提现明细
     */
    private void showWithdrawalList() {
        billDetails = new ArrayList<>();
        String url = UrlUtil.URL + "/commission/detail?type=2";
        RequestQueue requestQueue= Volley.newRequestQueue(BillDetailsActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    Map<String, Object> map = JsonUtil.jsonToMap(s);
                    Integer code = (Integer) map.get("code");
                    if (code == 0) {
                        List<Map<String, Object>> mapList = (List<Map<String, Object>>) map.get("data");
                        Log.i(TAG,mapList.toString());
                        for (int i = 0; i < mapList.size(); i++) {
                            String id = (String) mapList.get(i).get("id");
                            String amount = (String) mapList.get(i).get("amount");
                            String created_at = (String) mapList.get(i).get("created_at");
                            String tail_number = (String) mapList.get(i).get("tail_number");
                            BillDetails details = new BillDetails();
                            details.setId(Integer.parseInt(id));
                            details.setName("提现到银行卡" + tail_number);
                            details.setMoney(amount);
                            details.setTime(created_at);
                            billDetails.add(details);
                        }

                    } else if(code == -1){
                        Toast.makeText(BillDetailsActivity.this, "登录失效，请重新登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(BillDetailsActivity.this, GetPhoneCodeActivity.class);
                        intent.putExtra("loginCode","-1");
                        startActivity(intent);
                        finish();
                    }else {
                        Toast.makeText(BillDetailsActivity.this, "提现明细列表获取失败", Toast.LENGTH_SHORT).show();
                        overridePendingTransition(R.anim.xpage_slide_in_left, R.anim.xpage_slide_out_right);
                        finish();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                bill_details_list.setAdapter(new BillWithdrawalAdapter(BillDetailsActivity.this,billDetails));
                RelativeLayout bill_details_loading = findViewById(R.id.bill_details_loading);
                RelativeLayout bill_details_main = findViewById(R.id.bill_details_main);
                bill_details_main.setVisibility(View.VISIBLE);
                bill_details_loading.setVisibility(View.INVISIBLE);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, volleyError.getMessage(), volleyError);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                String sesseionid = new ConnectViaSession(BillDetailsActivity.this).GetSession();
                if (!sesseionid.equals("")) {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("cookie", sesseionid);
                    return headers;
                } else {
                    return super.getHeaders();
                }
            }
        };

        requestQueue.add(stringRequest);


    }

    /**
     * 展示收益明细
     */
    private void showDetailList() {
        billDetails = new ArrayList<>();
        String url = UrlUtil.URL + "/commission/detail?type=1";
        RequestQueue requestQueue= Volley.newRequestQueue(BillDetailsActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    Map<String, Object> map = JsonUtil.jsonToMap(s);
                    Integer code = (Integer) map.get("code");
                    if (code == 0) {
                        List<Map<String, Object>> mapList = (List<Map<String, Object>>) map.get("data");
                        Log.i(TAG,mapList.toString());
                        for (int i = 0; i < mapList.size(); i++) {
                            String id = (String) mapList.get(i).get("id");
                            String amount = (String) mapList.get(i).get("amount");
                            String created_at = (String) mapList.get(i).get("created_at");
                            String source = (String) mapList.get(i).get("source");
                            String typeName = (String) mapList.get(i).get("typeName");
                            BillDetails details = new BillDetails();
                            details.setId(Integer.parseInt(id));
                            details.setName(source +  "通过你的视频" + typeName);
                            details.setMoney(amount);
                            details.setTime(created_at);
                            billDetails.add(details);
                        }

                    } else if(code == -1){
                        Toast.makeText(BillDetailsActivity.this, "登录失效，请重新登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(BillDetailsActivity.this, GetPhoneCodeActivity.class);
                        intent.putExtra("loginCode","-1");
                        startActivity(intent);
                        finish();
                    }else {
                        Toast.makeText(BillDetailsActivity.this, "收益明细列表获取失败", Toast.LENGTH_SHORT).show();
                        overridePendingTransition(R.anim.xpage_slide_in_left, R.anim.xpage_slide_out_right);
                        finish();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                bill_details_list.setAdapter(new BillDetailsAdapter(BillDetailsActivity.this,billDetails));
                RelativeLayout bill_details_loading = findViewById(R.id.bill_details_loading);
                RelativeLayout bill_details_main = findViewById(R.id.bill_details_main);
                bill_details_main.setVisibility(View.VISIBLE);
                bill_details_loading.setVisibility(View.INVISIBLE);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, volleyError.getMessage(), volleyError);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                String sesseionid = new ConnectViaSession(BillDetailsActivity.this).GetSession();
                if (!sesseionid.equals("")) {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("cookie", sesseionid);
                    return headers;
                } else {
                    return super.getHeaders();
                }
            }
        };

        requestQueue.add(stringRequest);


    }

    /**
     * 切换提现明细
     */
    private void billWithdrawalChange() {
        bill_withdrawal_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bill_withdrawal_text.setTextColor(Color.parseColor("#333333"));
                bill_withdrawal_img.setVisibility(View.VISIBLE);
                bill_details_img.setVisibility(View.INVISIBLE);
                bill_details_text.setTextColor(Color.parseColor("#999999"));
                showWithdrawalList();
            }
        });
    }

    /***
     * 切换收益明细
     */
    private void billDetailsChange() {
       bill_details_text.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               bill_details_text.setTextColor(Color.parseColor("#333333"));
               bill_details_img.setVisibility(View.VISIBLE);
               bill_withdrawal_img.setVisibility(View.INVISIBLE);
               bill_withdrawal_text.setTextColor(Color.parseColor("#999999"));
               showDetailList();
           }
       });

    }


    private void init() {
        bill_details_text = findViewById(R.id.bill_details_text);
        bill_withdrawal_text = findViewById(R.id.bill_withdrawal_text);
        bill_details_img = findViewById(R.id.bill_details_img);
        bill_withdrawal_img = findViewById(R.id.bill_withdrawal_img);
        bill_details_list = findViewById(R.id.bill_details_list);

        Intent intent = getIntent();
        String mode = intent.getStringExtra("mode");
        if(!mode.equals("details")){
            bill_details_text.setTextColor(Color.parseColor("#333333"));
            bill_details_img.setVisibility(View.VISIBLE);
            bill_withdrawal_img.setVisibility(View.INVISIBLE);
            bill_withdrawal_text.setTextColor(Color.parseColor("#999999"));
            showDetailList();
        }else {
            bill_withdrawal_text.setTextColor(Color.parseColor("#333333"));
            bill_withdrawal_img.setVisibility(View.VISIBLE);
            bill_details_img.setVisibility(View.INVISIBLE);
            bill_details_text.setTextColor(Color.parseColor("#999999"));
            showWithdrawalList();
        }


    }

    private void billDetailsReturn() {
        bill_details_return = findViewById(R.id.bill_details_return);
        bill_details_return = findViewById(R.id.bill_details_return);
        bill_details_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                overridePendingTransition(R.anim.xpage_slide_in_left,R.anim.xpage_slide_out_right);
                finish();
            }
        });
    }

}
